package timesheet.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import timesheet.model.Badging;
import timesheet.model.Event;
import timesheet.model.EventTime;
import timesheet.model.Project;
import timesheet.model.Users;
import timesheet.repository.BadgingRepository;
import timesheet.repository.EventRepository;
import timesheet.repository.EventTimeRepository;
import timesheet.repository.ProjectRepository;
import timesheet.repository.UserRepository;
import timesheet.web.ContextUtil;

@Controller
@Transactional
public class WorkHoursController {
	
	@Autowired UserRepository userRepository;
	@Autowired EventRepository eventRepository;
	@Autowired BadgingRepository badgingRepository;
	@Autowired EventTimeRepository eventTimeRepository;
	@Autowired ProjectRepository projectRepository;
	//add a parameter dfrom and dto
	@RequestMapping("/userworkhours")
	public ModelAndView userworkhours(){
		if((Long)ContextUtil.getHttpSession().getAttribute("UserId")!=null){
			ModelAndView mv = new ModelAndView("userworkhours");
			Date start = new Date();
			start.setHours(0);start.setMinutes(0);start.setSeconds(0);
			Date stop = new Date();
			stop.setHours(23);stop.setMinutes(59);stop.setSeconds(59);
			SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String datestart = sdf.format(start);
			String datestop = sdf.format(stop);
			mv.addObject("datestart", datestart);
			mv.addObject("datestop", datestop);
			addList(mv, false,false,false,false);
			return mv;
		}
		else{return new ModelAndView("home");}
	}
	@RequestMapping("/getpresencehours")
	public ModelAndView getpresencehours(@RequestParam("datepicker") String datestart, @RequestParam("datepicker2") String datestop, @RequestParam(value="selecteduser", required=false)String[] userslist) throws ParseException{
		if((Long)ContextUtil.getHttpSession().getAttribute("UserId")!=null){
			Long idUser = (Long)ContextUtil.getHttpSession().getAttribute("UserId");
			String userlistconditon =" and b.u.id = "+idUser+"";
			if (userslist != null) {
				userlistconditon =" and b.u.id in(";
			    for(String item: userslist){
			       userlistconditon+=item+",";
			       System.out.println(item);
			    }
				userlistconditon = userlistconditon.substring(0, userlistconditon.length() - 1);
				userlistconditon+=")"; 
			}
			ModelAndView mv = new ModelAndView("userworkhours");
			java.util.Date start = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(""+datestart);
			java.util.Date stop = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(""+datestop);
			List<Badging> badginglist = badgingRepository.getAllBadgingByDate(start, stop, userlistconditon);
			addList(mv, true,false,false,false);
			mv.addObject("datestart", datestart);
			mv.addObject("datestop", datestop);
			mv.addObject("badginglist", badginglist);
			return mv;
		}
		else{return new ModelAndView("home");}
	}
	
	//get each event time by user
	//change eventlist by planned list in workhours jsp
	//get here all eventTime by event by user
	@RequestMapping("/getworkhours")
	public ModelAndView getworkhours(@RequestParam("datepicker3") String datestart, @RequestParam("datepicker4") String datestop, @RequestParam(value="selecteduser", required=false)String[] userslist, @RequestParam(value="selectedproject", required=false)String[] projectlist) throws ParseException{
		if((Long)ContextUtil.getHttpSession().getAttribute("UserId")!=null){
			Long idUser = (Long)ContextUtil.getHttpSession().getAttribute("UserId");
			String userlistconditon =" and e.e.u.id = "+idUser+"";
			if (userslist != null) {
				userlistconditon =" and e.e.u.id in(";
			    for(String item: userslist){
			       userlistconditon+=item+",";
			       System.out.println(item);
			    }
				userlistconditon = userlistconditon.substring(0, userlistconditon.length() - 1);
				userlistconditon+=")"; 
			}
			String projectCondition = "";
			if(projectlist != null){
				projectCondition = " and e.e.p.id in(";
				for(String proj: projectlist){
					projectCondition+="\'"+proj+"\',";
				       System.out.println(proj);
				    }
				projectCondition = projectCondition.substring(0, projectCondition.length() - 1);
				projectCondition+=") "; 
			}
			ModelAndView mv = new ModelAndView("userworkhours");
			java.util.Date start = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(""+datestart);
			java.util.Date stop = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(""+datestop);
			List<EventTime> eventlist = eventTimeRepository.getAllEventTimeByDate(start, stop, userlistconditon, projectCondition);
			addList(mv, false,true,false,false);
			mv.addObject("eventlist", eventlist);
			mv.addObject("datestart", datestart);
			mv.addObject("datestop", datestop);
			return mv;
		}
		else{return new ModelAndView("home");}
	}
	@RequestMapping("/getplannedevent")
	public ModelAndView getPlannedEvent(@RequestParam("datepicker5") String datestart, @RequestParam("datepicker6") String datestop, @RequestParam(value="selecteduser", required=false)String[] userslist, @RequestParam(value="selectedproject", required=false)String[] projectlist) throws ParseException{
		if((Long)ContextUtil.getHttpSession().getAttribute("UserId")!=null){
			Long idUser = (Long)ContextUtil.getHttpSession().getAttribute("UserId");
			String userlistconditon =" and e.u.id = "+idUser+"";
			if (userslist != null) {
				userlistconditon =" and e.u.id in(";
			    for(String item: userslist){
			       userlistconditon+=item+",";
			       System.out.println(item);
			    }
				userlistconditon = userlistconditon.substring(0, userlistconditon.length() - 1);
				userlistconditon+=")"; 
			}
			String projectCondition = "";
			if(projectlist != null){
				projectCondition = " and e.p.id in(";
				for(String proj: projectlist){
					projectCondition+="\'"+proj+"\',";
				       System.out.println(proj);
				    }
				projectCondition = projectCondition.substring(0, projectCondition.length() - 1);
				projectCondition+=") "; 
			}
			
			ModelAndView mv = new ModelAndView("userworkhours");
			java.util.Date start = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(""+datestart);
			java.util.Date stop = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(""+datestop);
			List<Event> plannedlist = eventRepository.getAllEventByDate(start, stop, userlistconditon, projectCondition);
			addList(mv, false,false,true,false);
			mv.addObject("datestart", datestart);
			mv.addObject("datestop", datestop);
			mv.addObject("plannedlist", plannedlist);
			return mv;
		}
		else{return new ModelAndView("home");}
	}
		
	
	@RequestMapping("/getuserfromproject")
	public ModelAndView getUserFromProjectDetail(@RequestParam(value="project", required=false)String[] project){
		if((Long)ContextUtil.getHttpSession().getAttribute("UserId")!=null){
			ModelAndView mv = new ModelAndView("userworkhours");
			String string = project[0];
			Long id = Long.parseLong(project[0]);
			Project p = projectRepository.find(id);
			List<Users> userName = eventTimeRepository.getUserNameByProject(p);
			List<EventTime> userByProject = eventTimeRepository.getUserByProject(p);
			Date start = new Date();
			start.setHours(0);start.setMinutes(0);start.setSeconds(0);
			Date stop = new Date();
			stop.setHours(23);stop.setMinutes(59);stop.setSeconds(59);
			SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String datestart = sdf.format(start);
			String datestop = sdf.format(stop);
			mv.addObject("datestart", datestart);
			mv.addObject("datestop", datestop);
				mv.addObject("username", userName);
				mv.addObject("userbyproject", userByProject);
				addList(mv, false,false,false,true);
				return mv;
		}
		else{return new ModelAndView("home");}
	}
	public void addList(ModelAndView mv, boolean ok, boolean ok2, boolean ok3, boolean ok4){
		List<Users> userList = userRepository.getAllUser();
		List<Project> projectList = projectRepository.getAllProject();
		mv.addObject("projectList", projectList);
		mv.addObject("userList", userList);
		mv.addObject("ok", ok);
		mv.addObject("ok2", ok2);
		mv.addObject("ok3", ok3);
		mv.addObject("ok4", ok4);
	}
}
